/**
 * 该类数据管理类;
 * 用于配置/设置/获取网络链接用到的服务器地址信息, 时差, 串号标志;
 */

class GLNetDataModel {
	platformCfg = null; //平台认证服务器配置
	// loginCfg: null;       //登录服务器配置
	gameHallCfg = null; //游戏大厅业务服务器配置
	defaultGameHallCfg = null; //默认大厅服地址;
	diffServerTime = null; //服务器与本地时间差;

	sendHeartTime = 0; //心跳发送时间(本地时间);
	serverTimeout = 0; //网络延迟时间(与服务器的);

	auth_type = 1; //登录账号类型; //cc.vv.GLDefine.EAuthType.GUEST;

	platform_ret_data = {
		//用于记录微信或其他平台返回信息;
		authType: 2, //cc.vv.GLDefine.EAuthType.WECHAT
		userId: "",
		password: "",
		refreshToken: "",
		unionId: "",
		name: "",
		sex: 2, //cc.vv.GLDefine.ESexType.FEMALE;
		avatar: "",
		is_update: false,
		updateDate: 0,
		game_id: 0
	};

	versionType = 0;

	init() {
		this.versionType = 0;
	}

	setVersionType(type) {
		this.versionType = type || 0;
		console.log("____versionType = " + this.versionType);
	}

	setPlatformCfg(cfg) {
		if (!this.platformCfg) {
			this.platformCfg = {};
		}
		this.platformCfg = cfg;

		//this.setVersionType(this.platformCfg.versionType);
	}

	getPlatformCfg() {
		if (!this.platformCfg) {
			this.platformCfg = {};

			var serverCfg;
			if (this.versionType) {
				serverCfg = cc.vv.serverList.serverList[cc.vv.GLConfig.game_debug_state][1];
			} else {
				serverCfg = cc.vv.serverList.serverList[cc.vv.GLConfig.game_debug_state][0];
			}
			this.platformCfg = serverCfg;
		}
		return this.platformCfg;
	}

	setGameHallCfg(ip, port) {
		if (!this.gameHallCfg) {
			this.gameHallCfg = {};
		}
		this.gameHallCfg.hall_ip = ip;
		this.gameHallCfg.hall_port = port;
	}

	getGameHallCfg() {
		if (!this.gameHallCfg) {
			cc.log("游戏登录服务器获取失败");
		}
		return this.gameHallCfg;
	}
	setDefaultGameHallCfg(host, port) {
		if (!this.defaultGameHallCfg) {
			this.defaultGameHallCfg = {};
		}
		this.defaultGameHallCfg.hall_ip = host;
		this.defaultGameHallCfg.hall_port = port;
	}
	/**
	 * 设置时差;
	 */
	setDiffServerTime(serverTime) {
		var now = new Date().getTime();
		var delta = serverTime - now;
		if (this.diffServerTime == null) {
			this.diffServerTime = delta;
		} else {
			var pre = Math.abs(this.diffServerTime);
			var cur = Math.abs(delta);
			if (cur < pre) {
				//raise precision
				this.diffServerTime = delta;
			}
		}
	}
	setSendTime() {
		var now = new Date();
		this.sendHeartTime = now.getTime();
	}
	/**
	 * 延时;
	 */
	setReceiveTime() {
		var now = new Date();
		var nowTime = now.getTime();
		this.serverTimeout = nowTime - this.sendHeartTime;
		//console.log('网络延时---------:' + this.serverTimeout);
		cc.vv.eventCustom.emit("hearttime", this.serverTimeout)
		if (this.serverTimeout > 600) {
			//cc.vv.GLUtils.showTips("您当前的网络状况很差,可能会影响游戏体验！");
		}
	}
	getServerTimeOut() {
		return this.serverTimeout;
	}

	getDiffServerTime() {
		return this.diffServerTime || 0;
	}

	setAuthType(type) {
		this.auth_type = type || cc.vv.GLDefine.EAuthType.GUEST;
	}
	getAuthType() {
		return this.auth_type;
	}
}

export let glNetDataModel: GLNetDataModel = new GLNetDataModel();
